/**
 * 系统后台页面配置配置JS-pageConfig.jsp
 */

//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";

$(document).ready(function(){
	//左侧tree数据绑定
	var setting = {	
		data: {
			key:{
				title:"hint"
			},
			simpleData: {
				enable: true
			}
		},
		callback: {
			onClick: onClick
		}
	};
	var zNodes =GetLeftTreeData();
	$.fn.zTree.init($("#tree"), setting, zNodes);
	
	//Tree点击事件
	function onClick(event, treeId, treeNode, clickFlag) {
		
		$('#myTab a[href="#tab_1_1_1"]').tab('show');
		$("#hid_IsMark").val(treeNode.isMark);
		if(treeNode.isMark == "1"){//新增数据表
			$("#dtConn").val(treeNode.id);
			$("#hid_dbkey").val(treeNode.id);
			bingDataTable(treeNode.id);
		}else if(treeNode.isMark == "2"){//修改数据表
			var cfgtable="";
			$.ajax({
		        type : 'POST',
		        url : postPath + '/system/dataConfig/getCfgTable',
		        data:{dtConfigId:treeNode.id},
		        async : false,
		        success : function(res) {
		        	cfgtable=res;
		        }
		    });
			bingDataTable(cfgtable.dtConn);
			$("#dtName").val(cfgtable.dtName).trigger("change");
			$("#dtConfigId").val(cfgtable.dtConfigId);
			$("#dtNameCn").val(cfgtable.dtNameCn);
			$("#dtOrder").val(cfgtable.dtOrder);
			$("#dtConn").val(cfgtable.dtConn);
			$("#dtPrimarykey").val(cfgtable.dtPrimarykey);
			$("#dtRemark").val(cfgtable.dtRemark);
			$("#hid_dtguid").val(cfgtable.guid);
			$("#hid_dtConfigId").val(cfgtable.dtConfigId);
			$("#dtName").attr("disabled","disabled");
			$("#dtConfigId").attr("disabled","disabled");
			$("#hid_dbkey").val(cfgtable.dtConn);
		}
				
	}
	
	//获取左侧列表数据
	function GetLeftTreeData()
	{
		var treeData;
		$.ajax({
	        type : 'POST',
	        url : postPath + '/system/group/getGroupForTree',
	        async : false,
	        cache:false,
	        success : function(res)  {
	        	treeData=res;
	        }
	    });
		return treeData;
	}
	
	//左侧树刷新操作
	$("#tree_refresh").click(function(){
		zNodes =GetLeftTreeData();
		$.fn.zTree.init($("#tree"), setting, zNodes);
	});
	
	//左侧树删除操作
	$("#tree_del").click(function(){
		if($("#dtConfigId").val() == ""){
			// bootbox.alert("请选择要删除的表");
			showMsg("请选择要删除的表!","warning");
			return;
		}
		bootbox.confirm("确认删除?", function(result) {
			if(result == false)
				return;
			 $.ajax({
	                url:  postPath + '/system/dataConfig/delTableInfo',
	                type: 'POST',
	                data: { dtConfigId: $("#dtConfigId").val() },
	                success: function (data) {
	                	if(data > 0){
	                		zNodes =GetLeftTreeData();
	                		$.fn.zTree.init($("#tree"), setting, zNodes);
	                		cleanTableInfo();
	                		showMsg("删除成功!","success");
	                	}else{
							showMsg("删除失败!","error");
	                	}
	                    
	                },
	                error:function()
	                {
						showMsg("删除失败,请刷新后重试!","error");
	                }
	            });
         }); 
	});
	
})

$("#btn_generator").click(function(){
	 var nodes = '';
	 var treeObj=$.fn.zTree.getZTreeObj("tree");
     nodes=treeObj.getSelectedNodes();
     //alert(nodes[0].id);
     if(nodes.length ==0 ){
		 showMsg("请选择一个系统进行生成!","warning");
    	 // bootbox.alert("请选择一个系统进行生成!");
    	 return ;
     }else if(nodes[0].fatureid !=0){
		 showMsg("请选择一个系统进行生成!","warning");
    	 // bootbox.alert("请选择一个系统进行生成!");
    	 return ;
     }
     
	 $.ajax({
         url:  postPath + '/system/pageConfig/generator',
         type: 'POST',
         data: { systemId: nodes[0].id },
         success: function (data) {
         	alert(data);
             
         },
         error:function()
         {
         	showMsg("删除失败,请刷新后重试!","error");
         }
     });
});

function showMsg(text, icon) {
	$.toast().reset('all');
	$.toast({
		text: text,
		icon: icon,
		showHideTransition: 'fade',
		allowToastClose: true,
		hideAfter: 1500,
		stack: false,
		position: 'top-right',
		textAlign: 'left',
		loader: true,
		loaderBg: '#9ec600',
	});
}
